﻿using Model;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using Utility.Helper;

namespace BLL
{
    public class QS_PLANMONTH_DETAILBLL : EntityBLL<QS_PLANMONTH_DETAIL>
    {
        public QS_PLANMONTH_DETAILBLL(Service service)
        {
            this.Service = service;
        }

        public QS_PLANMONTH_DETAILBLL()
        {
        }

        public override bool Insert(QS_PLANMONTH_DETAIL entity)
        {
            entity.N_MDETAIL_ID = GetSequence("SEQ_MDETAIL_ID");
            return base.Insert(entity);
        }

        public QS_PLANMONTH_DETAIL Find(int applyId, int proId)
        {
            return Where("N_MONTHAPP_ID=" + applyId + " AND N_PROID=" + proId).FirstOrDefault();
        }

        public override bool Update(QS_PLANMONTH_DETAIL entity)
        {
            var sql = @"UPDATE QS_PLANMONTH_DETAIL SET N_MONTHAPP_ID={0},N_PROID={1},
C_PRONAME='{2}',N_AMOUNT={3},C_REMARK='{4}',N_OPERATORID={5},D_OPERATORTIME=sysdate,
N_BUSINESS_ID={7},N_TYPE={8},N_SYAMOUNT={9},N_USEAMOUNT={10} WHERE N_MDETAIL_ID={11}";
            sql = string.Format(sql, entity.N_MONTHAPP_ID, entity.N_PROID, entity.C_PRONAME, entity.N_AMOUNT,
                entity.C_REMARK == null ? "" : "" + entity.C_REMARK + "", entity.N_OPERATORID, entity.D_OPERATORTIME.ToString("yyyy-MM-dd HH:mm:ss"), entity.N_BUSINESS_ID, entity.N_TYPE, entity.N_SYAMOUNT, entity.N_USEAMOUNT, entity.N_MDETAIL_ID);
            return NoQuery(sql) > 0 ? true : false;
        }

        public override QS_PLANMONTH_DETAIL Find(object key)
        {
            return Where("N_MDETAIL_ID=" + key.ToString()).FirstOrDefault();
        }

        public List<QS_PLANMONTH_DETAIL> GetByApplyId(decimal id)
        {
            var sql = @"SELECT
                            T1.*,T2.C_DETAILS_CODE,NULL AS C_IMAGE_URL
                        FROM  QS_PLANMONTH_DETAIL T1,QS_PROJECT T2
                        WHERE T1.N_PROID = T2.N_PROID AND T1.N_MONTHAPP_ID = :APPLY_ID
                        ORDER BY T1.N_MDETAIL_ID";
            var parms = new List<Tuple<string, string, string>>();
            parms.Add(":APPLY_ID", id, "int");
            var entitys = ConvertEntity(Query(sql, parms));
            return entitys;
        }

        public JArray GetByApplyIdToJArray(decimal id)
        {
            var sql = @"SELECT
                            T1.*,T2.C_DETAILS_CODE
                        FROM  QS_PLANMONTH_DETAIL T1,QS_PROJECT T2
                        WHERE T1.N_PROID = T2.N_PROID AND T1.N_MONTHAPP_ID = :APPLY_ID
                        ORDER BY T1.N_MDETAIL_ID";
            var parms = new List<Tuple<string, string, string>>();
            parms.Add(":APPLY_ID", id, "int");
            var entitys = JArray.FromObject(Query(sql, parms));
            return entitys;
        }
    }
}